package com.qcq.es.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcq.es.ESUtil.ESUtils;
import com.qcq.es.entity.PageResult;
import com.qcq.es.entity.QueryPageBean;
import com.qcq.es.mapper.GoodsMapper;
import com.qcq.es.pojo.vo.GoodsVO;
import com.qcq.es.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qcq
 * @date ：Created in 2020/11/23 22:48
 * @description：
 */
@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsVO> implements GoodsService{



//
//    @Override
//    public PageResult search(Integer currentPage, Integer pageSize, String keyword) {
//        PageResult pageResult = new PageResult();
//        try {
//            ESUtils esUtils = new ESUtils();
//            Integer from = (currentPage - 1) * pageSize;
//
//            //SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            //如果keyword为空则分页查询全部，否则进行条件分页模糊查询
//            if (StringUtils.isBlank(keyword)) {
//                MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
//                pageResult = esUtils.findPage(matchAllQuery, from, pageSize);
//            } else {
//                FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("name", keyword);
//                queryBuilder.fuzziness(Fuzziness.TWO);
//                pageResult = esUtils.findPage(queryBuilder, from, pageSize);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return pageResult;
//    }

    @Override
    public PageResult search(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String keyword = queryPageBean.getQueryString();
        Integer conId = queryPageBean.getConId();
        Integer priceId = queryPageBean.getPriceId();
        Integer priceSort = queryPageBean.getPriceSort();
        PageResult pageResult = new PageResult();
        try {
            ESUtils esUtils = new ESUtils();
            Integer from = (currentPage - 1) * pageSize;
            //SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //如果keyword为空则分页查询全部，否则进行条件分页模糊查询
            if (StringUtils.isBlank(keyword)) {
                MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
                pageResult = esUtils.findPage(matchAllQuery, from, pageSize,keyword,conId,priceSort);
            } else {
                BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
                /*FuzzyQueryBuilder fuzzyBuilder = QueryBuilders.fuzzyQuery("name", keyword);
                fuzzyBuilder.fuzziness(Fuzziness.TWO);
                queryBuilder.must(fuzzyBuilder);*/
                TermQueryBuilder termNameBuilder = QueryBuilders.termQuery("name", keyword);
                queryBuilder.must(termNameBuilder);
//类目名称
                TermQueryBuilder termCategoryQueryBuilder = QueryBuilders.termQuery("categoryName", keyword);
                queryBuilder.should(termCategoryQueryBuilder);
//品牌名称
                TermQueryBuilder termBrandQueryBuilder = QueryBuilders.termQuery("brandName", keyword);
                queryBuilder.should(termBrandQueryBuilder);

                if (priceId != -1) {
                    RangeQueryBuilder rangeBuilder = QueryBuilders.rangeQuery("price");
                    switch (priceId) {
                        case 1://0-500元
                            rangeBuilder.gte(0);
                            rangeBuilder.lt(500);
                            break;
                        case 2://500-1000元
                            rangeBuilder.gte(500);
                            rangeBuilder.lt(1000);
                            break;
                        case 3://1000-1500元
                            rangeBuilder.gte(1000);
                            rangeBuilder.lt(1500);
                            break;
                        case 4://1500-2000元
                            rangeBuilder.gte(1500);
                            rangeBuilder.lt(2000);
                            break;
                        case 5://2000-3000元
                            rangeBuilder.gte(2000);
                            rangeBuilder.lt(3000);
                            break;
                        case 6://3000元以上
                            rangeBuilder.gte(3000);
                            //rangeBuilder.lt(3000);
                            break;
                        default:
                            break;
                    }
                    queryBuilder.must(rangeBuilder);
                }

                pageResult = esUtils.findPage(queryBuilder, from, pageSize,keyword,conId,priceSort);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

//    @Override
//    public PageResult search(Integer currentPage, Integer pageSize) {
//        PageResult pageResult = new PageResult();
//        try {
//            ESUtils esUtils = new ESUtils();
//            currentPage = (currentPage - 1) * pageSize;
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
//            //if (StringUtils.isBlank(keyword)) {
//                pageResult = esUtils.queryAll(matchAllQuery, currentPage, pageSize);
//            //}
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return pageResult;
//    }





}
